/**
 * Created by huachuan on 2015/12/26.
 */
angular.module('newscontrollers', [])
  .controller("NewsCtrl",function($scope,$state,$rootScope,$ionicSlideBoxDelegate,Loading,ENV,News){
    //图片路径
    $scope.picPath = ENV.picPath;
      //测试数据
      $scope.newsBottomList1 =[{
          title:'美酒人生',
          list:[{
              "message_time": 1493258450000,
              "message_icon": "f9b36f5a-2c2f-4137-9fea-dd43512b3bc8.jpg",
              "message_title": "宁夏葡萄酒品酒师协会正式成立",
              "message_id": 670,
              "message_type": 0,
              "message_summary": "4月24日，宁夏葡萄酒品酒师协会经宁夏回族自治区民政厅批准，正式成立。"
          },
              {
                  "message_time": 1493258138000,
                  "message_icon": "0a9daafd-e3da-42a4-959c-91982e0cc910.jpg",
                  "message_title": "桃红葡萄酒节5首登日本东京",
                  "message_id": 669,
                  "message_type": 0,
                  "message_summary": "2017年，活动首次登陆日本东京。"
              },
              {
                  "message_time": 1493173722000,
                  "message_icon": "044e0f2f-9ee0-4c6c-8ee3-9a9427c27fec.jpg",
                  "message_title": "佛罗里达州将取消酒瓶大小限制",
                  "message_id": 668,
                  "message_type": 0,
                  "message_summary": "佛罗里达州州长Sen. Jeff Brandes将颁布最新法律修正案。"
              }]
      },
          {
              title:'酒识人生',
              list:[{
                  "message_time": 1493258450000,
                  "message_icon": "f9b36f5a-2c2f-4137-9fea-dd43512b3bc8.jpg",
                  "message_title": "宁夏葡萄酒品酒师协会正式成立",
                  "message_id": 670,
                  "message_type": 0,
                  "message_summary": "4月24日，宁夏葡萄酒品酒师协会经宁夏回族自治区民政厅批准，正式成立。"
              },
                  {
                      "message_time": 1493258138000,
                      "message_icon": "0a9daafd-e3da-42a4-959c-91982e0cc910.jpg",
                      "message_title": "桃红葡萄酒节5首登日本东京",
                      "message_id": 669,
                      "message_type": 0,
                      "message_summary": "2017年，活动首次登陆日本东京。"
                  },
                  {
                      "message_time": 1493173722000,
                      "message_icon": "044e0f2f-9ee0-4c6c-8ee3-9a9427c27fec.jpg",
                      "message_title": "佛罗里达州将取消酒瓶大小限制",
                      "message_id": 668,
                      "message_type": 0,
                      "message_summary": "佛罗里达州州长Sen. Jeff Brandes将颁布最新法律修正案。"
                  }]
          }
      ];
    $scope.$on('$ionicView.beforeEnter', function () {
      $rootScope.showWineGlasses = false;
    });
    //获取新闻列表
    Loading.showLoading();
    News.getNewsList();
    //读取资讯
    $scope.readMessage = function(messageId,fromWhere){
        News.saveFromWhere(fromWhere);
        $state.go('newsDetails',{'messageId':messageId});
    }
    //获取网络数据成功的后调用该方法
    $scope.$on('News.newsUpdated', function(){
      //隐藏加载框
      Loading.hideLoading();
      $scope.newsTopList = News.getTopNewsListJson();
      $scope.newsBottomList = News.getBottomNewsListJson();
      $scope.$broadcast('scroll.refreshComplete');
      //$ionicSlideBoxDelegate.$getByHandle('myHandle').update();
      $ionicSlideBoxDelegate.update();
    });
    $scope.doRefresh = function(){
      News.getNewsList();
    }
    $scope.loadMore = function(){
      News.getMoreNewsList();
    }
    $scope.$on('News.MoreNewsUpdated',function(){
      $scope.newsBottomList = News.getBottomNewsListJson();
      $scope.$broadcast('scroll.infiniteScrollComplete');
    });
    $scope.hasNextPage = function(){
      return News.hasNextPage();
    }
  });
